package com.lucy.rabc.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.Hibernate;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
import java.util.Objects;
import java.util.Set;

/**
 * @author kindear
 */
@Getter
@Setter
@ToString
@RequiredArgsConstructor
@Entity
@Table(name = "sys_menu")
@ApiModel(value = "系统菜单实体",description = "用户菜单管理")
@EntityListeners(AuditingEntityListener.class)
public class SysMenu implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @ApiModelProperty(value = "菜单ID",name = "id")
    Long id;

    @ApiModelProperty("菜单名称")
    @Column(name = "menu_name")
    String menuName;

    /** 父菜单 **/
    @JsonIgnoreProperties(value = {"children","parent"})
    @ApiModelProperty("父菜单ID")
    @ManyToOne(cascade={CascadeType.MERGE,CascadeType.REFRESH},optional=true)
    @JoinColumn(name="parent_id")
    SysMenu parent;

    /** 父 菜单名称 **/
    @ApiModelProperty("父菜单名称")
    @Column(name = "parent_name")
    String parentName;



    /** 菜单显示顺序 **/
    @ApiModelProperty("菜单排序")
    @Column(name = "menu_order")
    Integer menuOrder;

    /** 路由地址 **/
    @ApiModelProperty("路由地址")
    @Column(name = "path")
    String path;

    /** 组件路径 **/
    @ApiModelProperty("组件路径")
    @Column(name = "component")
    String component;


    /** 类型（M目录 P页面 B按钮） */
    @ApiModelProperty("菜单类型")
    @Column(name = "menu_type")
    private String menuType;

    /** 权限字符串 */
    @ApiModelProperty("权限要求")
    @Column(name = "perms")
    private String perms;

    /** 菜单图标 **/
    @ApiModelProperty("菜单图标")
    @Column(name = "icon")
    String icon;

    /**
     * 子菜单Id列表
     */
    @JsonIgnoreProperties(value = {"parent"})
    @OneToMany(mappedBy = "parent",cascade=CascadeType.REMOVE,fetch=FetchType.LAZY)
    @ToString.Exclude
    Set<SysMenu> children;


    @CreatedDate
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @ApiModelProperty(value = "记录创建时间",name = "createdTime")
    @Column(name = "created_time")
    private Date createdTime;
    @LastModifiedDate
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @ApiModelProperty(value = "记录更新时间",name = "updatedTime")
    @Column(name = "updated_time")
    private Date updatedTime;

    public void sourceParent(SysMenu sysMenu){
        this.parent = sysMenu;
        this.parentName = sysMenu.getMenuName();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
            return false;
        }
        SysMenu sysMenu = (SysMenu) o;
        return id != null && Objects.equals(id, sysMenu.id);
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }
}
